<?php 
    session_start();
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
                
        <link rel="stylesheet" href="styles/layout.css" type="text/css"/>
        <link rel="stylesheet" href="styles/computer.css" type="text/css"/>
        <script type="text/javascript" src="scripts/computer.js"></script>
        
        <script type="text/javascript">
            var xmlhttp;
            var productID;
            var categoryID;
            var saleID;
            var i, j, k;
            
            function deleteProduct(){
                var productIDRB = document.sellerProduct.productID;
                for(i = 0; i < productIDRB.length; i++){
                    if(productIDRB[i].checked){
                        productID = productIDRB[i].value;
                        break;
                    }
                }
                
                if(window.XMLHttpRequest){
                    //code for IE7+, Firefox, Chrome, and Safari
                    xmlhttp = new XMLHttpRequest();
                }else{
                    //code for IE6 and IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                
                xmlhttp.onreadystatechange = handleReplyProduct;
                xmlhttp.open("GET", "deleteProduct.php?productID="+productID, true);
                xmlhttp.send();
            }
            
            function handleReplyProduct(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                    alert(xmlhttp.responseText);
                    productTable = document.getElementById("productTable");
                    productTable.deleteRow(i+1);
                }
            }
            
            function deleteCategory(){
                var categoryIDRB = document.sellerCategory.categoryID;
                for(j = 0; j < categoryIDRB.length; j++){
                    if(categoryIDRB[j].checked){
                        categoryID = categoryIDRB[j].value;
                        break;
                    }
                }
                
                if(window.XMLHttpRequest){
                    //code for IE7+, Firefox, Chrome, and Safari
                    xmlhttp = new XMLHttpRequest();
                }else{
                    //code for IE6 and IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                
                xmlhttp.onreadystatechange = handleReplyCategory;
                xmlhttp.open("GET", "deleteCategory.php?categoryID="+categoryID, true);
                xmlhttp.send();                
            }
            
            function handleReplyCategory(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                    alert(xmlhttp.responseText);
                    categoryTable = document.getElementById("categoryTable");
                    categoryTable.deleteRow(j+1);
                }                
            }
            
            function deleteSale(){
                var saleIDRB = document.sellerSale.saleID;
                for(k = 0; k < saleIDRB.length; k++){
                    if(saleIDRB[k].checked){
                        saleID = saleIDRB[k].value;
                        break;
                    }
                }
                
                if(window.XMLHttpRequest){
                    //code for IE7+, Firefox, Chrome, and Safari
                    xmlhttp = new XMLHttpRequest();
                }else{
                    //code for IE6 and IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                
                xmlhttp.onreadystatechange = handleReplySale;
                xmlhttp.open("GET", "deleteSale.php?saleID="+saleID, true);
                xmlhttp.send();                
                
            }
            
            function handleReplySale(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                    alert(xmlhttp.responseText);
                    saleTable = document.getElementById("saleTable");
                    saleTable.deleteRow(k+1);
                }                 
            }
            
            
        </script>
    </head>
    <body>
        <?php        require 'header.html';
        ?>
        
<div id="container">
  <div class="wrapper">
    <div id="content">
    	<div id="order">        
        <?php
        if(isset($_SESSION["userID"]) && !strcmp($_SESSION["userType"], "seller")){
        
        echo "Login: ".$_SESSION["userID"]." ".$_SESSION["userType"];
        echo "<br/>Welcome ".$_COOKIE["username"];
        
        // put your code here
        $con = mysql_connect("localhost", "zhouzhao", "19831022");
        if(!$con){
            die("could not connect to DB: ".mysql_error());
        }
        
        mysql_select_db("cloudcom", $con);
        $result = mysql_query("select * from products");
        
        echo "<h3>Product Table</h3>";
        echo "<form method='post' name='sellerProduct' action='updateProduct.php'>";
        echo "<table border='1' id='productTable'>
            <tr>
                <th>CategoryID</th>
                <th>Product name</th>
                <th>Product description</th>
                <th>Product image</th>
                <th>Product price</th>
                <th>Select</th>
            </tr>";
        
        while($row = mysql_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>".$row["categoryID"]."</td>";
            echo "<td>".$row["productName"]."</td>";
            echo "<td>".$row["productDescription"]."</td>";
            echo "<td>".$row["productImage"]."</td>";
            echo "<td>".$row["productPrice"]."</td>";
            echo "<td><input type='radio' name='productID' value='".$row["productID"]."'/></td>";
            echo "</tr>";
        }
        echo "</table>";
        echo "<input type='submit' value='Add or Change Product' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)'/>";
        echo "<input type='button' value='Delete Product' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)' onclick='deleteProduct()'/>";
        echo "</form><br/><br/>";
        
        $result = mysql_query("select * from categories");
        
        echo "<h3>Category Table</h3>";
        echo "<form method='post' name='sellerCategory' action='updateCategory.php'>";
        echo "<table border='1' id='categoryTable'>
            <tr>
                <th>CategoryID</th>
                <th>Category name</th>
                <th>Category description</th>
                <th>Select</th>
            </tr>";
        
        while($row = mysql_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>".$row["categoryID"]."</td>";
            echo "<td>".$row["categoryName"]."</td>";
            echo "<td>".$row["categoryDescription"]."</td>";
            echo "<td><input type='radio' name='categoryID' value='".$row["categoryID"]."'/></td>";
            echo "</tr>";
        }
        echo "</table>";
        echo "<input type='submit' value='Add or Change Category' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)'/>";
        echo "<input type='button' value='Delete Category' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)' onclick='deleteCategory()'/>";
        echo "</form><br/><br/>";
        
        $result = mysql_query("select * from sales");
        
        echo "<h3>Sale Table</h3>";
        echo "<form method='post' name='sellerSale' action='updateSale.php'>";
        echo "<table border='1' id='saleTable'>
            <tr>
                <th>ProductID</th>
                <th>Discount</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Select</th>
            </tr>";
        
        while($row = mysql_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>".$row["productID"]."</td>";
            echo "<td>".$row["discount"]."</td>";
            echo "<td>".$row["startDate"]."</td>";
            echo "<td>".$row["endDate"]."</td>";
            echo "<td><input type='radio' name='saleID' value='".$row["saleID"]."'/></td>";
            echo "</tr>";
        }
        echo "</table>";
        echo "<input type='submit' value='Add or Change Sale' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)'/>";
        echo "<input type='button' value='Delete Sale' class='buttonStyle' onmouseover='changeOn(this)' onmouseout='changeOff(this)' onclick='deleteSale()'/>";
        echo "</form>";
        
        mysql_close($con);
        }else{
            require 'login.html';
        }
        ?>
       </div>	
    </div>
    <br class="clear" />
  </div>
</div>
        <?php        require 'tail.html';
        ?>
        
    </body>
</html>
